<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Image extends Admin_Controller {
	
 	function __construct(){
	 	parent::__construct();
	 	$this->admin_navigation->parent_active('images');
	 }

	 function image_manager(){
	 	$this->load->model('images/image_model');
	 	$this->load->model('images/image_type_model');
		$columns = array(
						array(
							'name' => 'ID',
							'type' => 'id',
							'index'=>'image_id',
							'sort_column'=>false
							),
						array(
							'name'=>$this->__('Name'),
							'type'=>'text',
							'filter'=>'name',
							'index'=>'name',
							'sort_column'=>true
						),
						array(
							'name'=>__('Type'),
							'type'=>'select',
							'index'=>'image_type_name',
							'filter'=>'image_type_id',
							'options'=>$this->image_type_model->get_images_types_options()
						),
						array(
							'name'=>__('Category'),
							'type'=>'select',
							'index'=>'category_name',
							'filter'=>'category_id',
							'options'=>$this->image_model->get_categories_options()
						),
						array(
							'name'=>$this->__('Created'),
							'type'=>'date',
							'index'=>'created',
						),
						array(
							'name'=>$this->__('Status'),
							'type'=>'publish',
							'filter'=>'status',
							'index'=>'status',
							'options'=>array(
											 '0'=>'Tắt',
											 '1'=>'Bật'
											)
						),
		);
		$this->grid->columns($columns);
		$this->grid->datasource('images/image_model','get_images');
		$this->grid->set_title($this->__('Image Manager'));
		$this->grid->add_button($this->__('Add New'),admin_url('*/image/add_image'),'add');
		$this->grid->set_row_action('edit_image');
		$this->grid->total_rows($this->image_model->get_count());
		$this->grid->add_mass_action('delete', array(
			'label'=>$this->__('Delete'),
			'url'      => admin_url('*/image/delete_image'),
            'confirm'  =>$this->__('Are you sure ?')
		));

		if ($this->input->is_ajax()){
			$this->output->set_output($this->grid->display());
		}else{
			$this->load->view('admin/grid');
		}
	}
	
	function edit_image(){
		$image_id = $this->input->get_param('id',false);
		$this->load->model('images/image_model');
		$this->load->model('images/image_type_model');
		if ($image_id) {
			$data = $this->image_model->get_image($image_id);
			$this->form->set_values($data);
		}
		$this->form->set_form_attributes(array(
			'id'=>'image_form',
			'action'=>admin_url('*/image/edit_post_image',array('id'=>$image_id)),
			'enctype'=>'multipart/form-data'
		));
		$this->form->add_filedset('ganeral',array(
			'legend'=>'Ganeral'
		));
		
		$this->form->add_filed('name','text',array(
			'label'=>$this->__('Name'),
			'name'=>'name',
			'required'=>true
		));
		
		$this->form->add_filed('alias','alias',array(
			'label'=>$this->__('Alias'),
			'name'=>'alias',
		));
		$this->form->add_filed('image_type_id','select',array(
			'label'=>$this->__('Image Type'),
			'name'=>'image_type_id',
			'values'=>$this->image_type_model->get_images_types_options()
		));
		$this->form->add_filed('cat_id','select',array(
			'label'=>$this->__('Category'),
			'name'=>'category_id',
			'required'=>true,
			'values'=>$this->image_model->get_categories_options()
		));

		$this->form->add_filed('status','select',array(
			'label'=>$this->__('Status'),
			'name'=>'status',
			'values'=>array('Tắt','Bật')
		));
		$this->form->add_filed('is_hot','select',array(
			'label'=>$this->__('Hot'),
			'name'=>'is_hot',
			'values'=>array('0'=>$this->__('No'),'1'=>$this->__('Yes'))
		));
		$this->form->add_filed('image','image',array(
			'label'=>$this->__('Thumb'),
			'name'=>'image',
		));
		$this->form->add_filed('filename','file',array(
			'label'=>$this->__('File'),
			'name'=>'filename',
		));
		$this->form->add_filed('introtext','editor',array(
			'label'=>$this->__('Introtext'),
			'name'=>'introtext'
		));
		
		$this->load->view('admin/form',array('form'=>$this->form->to_html()));
	}
	
	function add_image(){
	 	$this->_forward('edit_image');
	}
	
	function delete_image(){
		$cids = explode(',',$this->input->get_param('image_id',false));
		$this->load->model('images/image_model');
		foreach ($cids as $cid){
			$this->image_model->delete_image($cid);
		}
		$this->_redirect(admin_url('*/image/image_manager'));
	}
	
	function edit_post_image(){
		$image_id = $this->input->get_param('id',false);
		$this->load->model('images/image_model');
		
		if (($data = $this->input->post())){
			
			$_file_name = strtolower(clean_entities(trim($data['name'])));
			
			$config['upload_path'] = './upload/images/';
			$config['allowed_types'] = '*';
			$config['file_name'] = $_file_name;
			
			$this->load->library('upload');
			$this->upload->initialize($config);
			
			if ($this->upload->do_upload('image')){
				if($this->upload->is_image()){
					$_img_config['image_library'] = 'gd2';
					$_img_config['source_image'] = 'upload/images/'.$this->upload->get_file_name();
					$_img_config['create_thumb'] = TRUE;
					$_img_config['maintain_ratio'] = TRUE;
					$_img_config['width'] = 60;
					$_img_config['height'] = 105;
					$this->load->library('image_lib', $_img_config); 
					$this->image_lib->resize();
				}
				$data['image'] = 'upload/images/'.$this->upload->get_file_name();
				//$data['filesize'] = $this->upload->get_width().' x '.$this->upload->get_height();
			}
			if ($this->upload->do_upload('filename')){
				if($this->upload->is_image()){
					$_img_config['image_library'] = 'gd2';
					$_img_config['source_image'] = 'upload/images/'.$this->upload->get_file_name();
					$_img_config['create_thumb'] = TRUE;
					$_img_config['maintain_ratio'] = TRUE;
					$_img_config['width'] = 60;
					$_img_config['height'] = 105;
					$this->load->library('image_lib', $_img_config); 
					$this->image_lib->resize();
				}
				$data['filename'] = 'upload/images/'.$this->upload->get_file_name();
				$data['filesize'] = $this->upload->get_width().' x '.$this->upload->get_height();
			}
			
			if ($image_id){
				$this->image_model->update_image($data,$image_id);
			}else{
				$this->image_model->insert_image($data);
			}
		}
		$this->_redirect(admin_url('*/image/image_manager'));
		
	}
	
	function type_image(){
		$this->load->model('images/image_model');
		$this->load->model('images/image_type_model');
		$columns = array(
						array(
							'name' => 'ID',
							'type' => 'id',
							'index'=>'image_type_id',
							'sort_column'=>true
							),
						array(
							'name'=>$this->__('Name'),
							'type'=>'text',
							'filter'=>'name',
							'index'=>'name',
							'sort_column'=>true
						),
						array(
							'name'=>__('Category'),
							'type'=>'select',
							'index'=>'category_name',
							'filter'=>'category_id',
							'options'=>$this->image_model->get_categories_options()
						),
						array(
							'name'=>$this->__('Created'),
							'type'=>'date',
							'index'=>'created',
						),
		);
		$this->grid->columns($columns);
		$this->grid->datasource('images/image_type_model','get_images_types');
		$this->grid->set_title($this->__('Image Type'));
		$this->grid->add_button($this->__('Add New'),admin_url('*/image/add_image_type'),'add');
		$this->grid->set_row_action('edit_image_type');

		$this->grid->total_rows($this->image_type_model->get_images_types_count());
		$this->grid->add_mass_action('delete', array(
			'label'=>$this->__('Delete'),
			'url'      => admin_url('*/image/delete_image_type'),
            'confirm'  =>$this->__('Are you sure ?')
		));
		if ($this->input->is_ajax()){
			$this->output->set_output($this->grid->display());
		}else{
			$this->load->view('admin/grid');
		}
	}
	
	function add_image_type(){
		$this->_forward('edit_image_type');
	}
	
	function edit_image_type(){
		$image_type_id = $this->input->get_param('id',false);
		$this->load->model('images/image_type_model');
		$this->load->model('images/image_model');
		if ($image_type_id){
			$data = $this->image_type_model->get_image_type_by_id($image_type_id);
			$this->form->set_values($data);
		}
		$this->form->set_form_attributes(array(
			'id'=>'edit_image_type_form',
			'action'=>admin_url('*/image/edit_image_type_post',array('id'=>$image_type_id)),
			'enctype'=>'multipart/form-data'
		));
		$this->form->add_filedset('ganeral',array('legend'=>'Ganeral'));
		
		$this->form->add_filed('name','text',array(
			'label'=>'Name',
			'name'=>'name',
			'required'=>true
		));
		
		$this->form->add_filed('alias','alias',array(
			'label'=>'Alias',
			'name'=>'alias',
			'required'=>false,
		));
		
		$this->form->add_filed('cat_id','select',array(
			'label'=>$this->__('Category'),
			'name'=>'category_id',
			'required'=>true,
			'values'=>$this->image_model->get_categories_options()
		));
		
		$this->form->add_filed('description','textarea',array(
			'label'=>'Description',
			'name'=>'description'
		));

		
		$data = array('form_title'=>'Add image Type','form'=>$this->form->to_html());
		$this->load->view('admin/form',$data);
	}
	
	function delete_image_type(){
		$cids = explode(',',$this->input->get_param('image_type_id'));
		if (count($cids)>0) {
			foreach ($cids as $cid){
				$this->load->model('images/image_type_model');
				$this->image_type_model->delete(array('image_type_id'=>$cid));
			}
		}
		$this->_redirect(admin_url('*/image/type_image'));
	}
	
	function edit_image_type_post(){
		$image_type_id = $this->input->get_param('id',false);
		$this->load->model('images/image_type_model');
		if (($data = $this->input->post())){
			if($image_type_id){
				$this->image_type_model->update($data,array('image_type_id'=>$image_type_id));
			}else{
				$this->image_type_model->save($data);
			}
		}
		$this->_redirect(admin_url('*/image/type_image'));
	}
	 function category_image(){
		$columns = array(
					array(
						'name' => 'ID',
						'type' => 'id',
						'index'=>'category_id',
						'sort_column'=>'desc'
						),
					array(
						'name'=>$this->__('Name'),
						'type'=>'text',
						'filter'=>'name',
						'index'=>'name',
						'sort_column'=>true
					),
					array(
						'name'=>$this->__('Created'),
						'type'=>'date',
						'index'=>'created',
					),
					array(
						'name'=>$this->__('Status'),
						'type'=>'publish',
						'filter'=>'status',
						'index'=>'status',
						'action'=>'publishcategory',
						'options'=>array(
										 '0'=>'Tắt',
										 '1'=>'Bật'
										)
					),
		);
		$this->grid->columns($columns);
		$this->grid->datasource('images/image_model','get_categories');
		$this->grid->set_title(__('Image Category'));
		$this->grid->add_button('Thêm mới',admin_url('*/image/add_category'),'add');
		$this->grid->set_row_action('edit_category');
		$this->grid->add_mass_action('delete',array(
												'label'=>__('Delete'),
												'url'      => admin_url('*/image/delete_category'),
            									'confirm'  =>__('Are you sure ?')
											));
		$this->grid->add_mass_action('status',array(
												'label'=>__('Status'),
												'url'=>admin_url('*/image/change_status_category'),
											));
		$this->load->model('images/image_model');
		$total_rows = $this->image_model->get_count_categories(array(),true);
		$this->grid->total_rows($total_rows);
		
		if ($this->input->is_ajax()){
			$this->output->set_output($this->grid->display());
		}else{
			$this->load->view('admin/grid');
		}
	}
	
	function edit_category(){
		$cat_id = $this->input->get_param('id',false);
		$this->load->model('images/image_model');
		if ($cat_id){
			$this->load->model('categories/categories_model');
			$data = $this->categories_model->get_category($cat_id);
			$this->form->set_values($data);
		}
		$this->form->set_form_attributes(array(
			'id'=>'edit_form',
			'action'=>admin_url('*/image/edit_post_category',array('id'=>$cat_id)),
			'enctype'=>'multipart/form-data'
		));
		$this->form->add_filedset('ganeral',array('legend'=>__('Ganeral')));
		
		$this->form->add_filed('name','text',array(
			'label'=>'Name',
			'name'=>'name',
			'required'=>true
		));
		
		$this->form->add_filed('alias','alias',array(
			'label'=>'Alias',
			'name'=>'alias',
			'required'=>false,
		));
		
		$this->form->add_filed('parent_id','select',array(
			'label'=>'Parent',
			'name'=>'parent_id',
			'values'=>$this->image_model->get_categories_options(array('other_category_id'=>$cat_id)),
		));
		$this->form->add_filed('image','image',array(
			'label'=>'Image',
			'name'=>'image',
		));
		$this->form->add_filed('status','select',array(
			'label'=>'Status',
			'name'=>'status',
			'values'=>array('Tắt','Bật')
		));
		$this->form->add_filed('description','editor',array(
			'label'=>'Description',
			'name'=>'description'
		));

		
		$data = array('form_title'=>'Add Category','form'=>$this->form->to_html());
		$this->load->view('admin/form',$data);
	}
	
	
	
	 function add_category(){
		$this->_forward('edit_category');
	}
	
	
	 function edit_post_category(){
	 	$cat_id = $this->input->get_param('id',false);
	 	$this->load->model('categories/categories_model');
	 	if ($cat_id){
	 		$this->categories_model->update_category($cat_id,$this->input->post());
	 	}else{
			$data = array_merge(array('module'=>'images'),$this->input->post());
			$this->categories_model->insert_category($data);
	 	}
		$this->_redirect(admin_url('*/image/image_category'));
	}
	
	function delete_category(){
		$cids = explode(',', $this->input->get_param('category_id'));
		if (count($cids)>0) {
			foreach ($cids as $cid){
				$this->load->model('images/image_model');
				$this->image_model->delete_category($cid);
			}
		}
		$this->_redirect(admin_url('*/image/image_category'));
	}
}
